programming4us
           
 
 
SQL Server

Programming with SQL Azure : Connecting to SQL Azure (part 2)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
1/3/2011 9:13:26 AM
1.2. Using a Data Reader

As you become more and more familiar with SQL Azure, you'll find that you don't need to make a lot of changes to your application code except possibly any inline T-SQL. The beauty of all this is that you're using a proven and trusted data-access technology, ADO.NET. Thus, nothing really changes. Let's modify the application and click event code to illustrate this. Follow these steps:

  1. Add a new list box to the form.

  2. In the click event, add the code in bold in the following snippet. This new code uses the SqlDataReader class to execute a simple SELECT command against the SQL Azure database and then iterate over the SqlDataReader to populate the list box:

    private void button1_Click(object sender, EventArgs e)
    {
    string connStr = GetConString();
    using (SqlConnection conn = new SqlConnection(connStr))
    {
    SqlCommand cmd = new SqlCommand("SELECT Name FROM Users", conn);
    conn.Open();
    SqlDataReader rdr = cmd.ExecuteReader();
    try

    {
    while (rdr.Read())
    {
    listBox1.Items.Add(rdr[0].ToString());
    }
    rdr.Close();
    }
    catch (SqlException ex)
    {
    MessageBox.Show(ex.Message.ToString());
    }
    }
    }

  3. Run the application, and click the button on the form. Within a few seconds, the list box populates with names from the Users table.

The key is that you can replace the connection string with your local connection string, and it still works. This is because you're using ADO.NET to handle the connection, and it doesn't care where the database is. Next, let's take this example one step further and look at how you use datasets.

1.3. Using a Dataset

In the last example, you found that there is no difference in using a SqlDataReader when querying a SQL Azure database. This example uses the SqlCommand class and the SqlDataAdapter to query SQL Azure and populate a dataset. Here are the steps:

  1. In the button's click event, replace the existing code with the following:

    using (SqlConnection conn = new SqlConnection(connStr))
    {
    try
    {
    using (SqlCommand cmd = new SqlCommand())
    {
    conn.Open();
    SqlDataAdapter da = new SqlDataAdapter();
    cmd.CommandText = "SELECT Name FROM Users";
    cmd.Connection = conn;
    cmd.CommandType = CommandType.Text;
    da.SelectCommand = cmd;
    DataSet ds = new DataSet("Users");
    da.Fill(ds);
    listBox1.DataSource = ds.Tables[0];
    listBox1.DisplayMember = "Name";
    }
    }
    catch (SqlException ex)
    {
    MessageBox.Show(ex.Message.ToString());
    }
    }


This code creates a new connection using the same connection information as the previous example, and then creates a new SqlCommand instance. The connection, text, and type of the SqlCommand are set and then executed using the instantiated SqlDataAdapter. A new dataset is created and filled from the SqlDataAdapter, which is then applied to the datasource property of the list box.

  1. Run the application, and click the button on the form. Again, the list box is populated with the names from the Users table in the SQL Azure database. Again, you could change the connection string to point to your local database and the code would work fine.

So, when would code like this not work? Suppose your application had code such as the following, which creates a table without a clustered index:

using (SqlConnection conn = new SqlConnection(connStr))
{
try
{
using (SqlCommand cmd = new SqlCommand())
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter();
cmd.CommandText = "CREATE TABLE TestTable(ID int, Name varchar(20))";
cmd.Connection = conn;
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO TestTable (ID, Name)
VALUES (1, 'Scott'), (2, 'Herve')";
int val = cmd.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message.ToString());
}
}

Although this code is certainly valid and runs successfully against your local SQL Server instance, it doesn't work when executing against your SQL Azure database. Why? Go ahead and replace the code in the button's click event with this code, and run the application. The error you get in the message box states that SQL Azure tables without a clustered index aren't supported. If you step through the code, you find out that the table is indeed created, but the error comes from trying to insert data into the table. You need to go through your application and look for these sorts of things, to ensure that the application will run successfully against SQL Azure.

We have discussed connecting with ADO.NET and the different options we have with ADO.NET, so let's move on to the other connection option, ODBC.

Other -----------------
- Programming with SQL Azure : Application Deployment Factors
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 3)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 2)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 1)
- SQL Server 2008: SQL Server Web Services
- SQL Server 2008: SQL Server Service Broker - Related System Catalogs
- SQL Azure Backup Strategies (part 2)
- SQL Azure Backup Strategies (part 1) - Copying a Database
- SQL Server 2008: Troubleshooting SSB Applications with ssbdiagnose.exe
- SQL Server 2008: Service Broker Routing and Security
- Migrating Databases and Data to SQL Azure (part 9)
- Migrating Databases and Data to SQL Azure (part 8)
- Understanding Service Broker Constructs (part 5)
- Understanding Service Broker Constructs (part 4) - Creating the Conversation Initiator
- Migrating Databases and Data to SQL Azure (part 7)
- Migrating Databases and Data to SQL Azure (part 6) - Building a Migration Package
- Migrating Databases and Data to SQL Azure (part 5) - Creating an Integration Services Project
- Understanding Service Broker Constructs (part 3)
- Understanding Service Broker Constructs (part 2) - Creating Queues for Message Storage
- Understanding Service Broker Constructs (part 1) - Defining Messages and Choosing a Message Type
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us